package com.ui.pickerview.view;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mywork.R;


/**
 * Created by Sai on 15/11/22.
 */
public class TimePickerView extends BasePickerView implements View.OnClickListener
{
	public enum Type
	{
		ALL, YEAR_MONTH_DAY, HOURS_MINS, MONTH_DAY_HOUR_MIN, YEAR_MONTH,SHX,YEAR_MONTH_DAY_HOUR
	}// 四种选择模式，年月日时分，年月日，时分，月日时分

	WheelTime wheelTime;
	private View btnSubmit, btnCancel;
	private TextView tvTitle;
	private LinearLayout timepicker;
	private static final String TAG_SUBMIT = "submit";
	private static final String TAG_CANCEL = "cancel";
	private OnTimeSelectListener timeSelectListener;

	public TimePickerView(Context context, Type type)
	{
		super(context);

		LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
		// -----确定和取消按钮
		btnSubmit = findViewById(R.id.btnSubmit);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		timepicker = (LinearLayout) findViewById(R.id.timepicker);
		btnCancel = findViewById(R.id.btnCancel);
		btnSubmit.setTag(TAG_SUBMIT);
		btnCancel.setTag(TAG_CANCEL);
		btnSubmit.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		// 顶部标题

		// ----时间转轮
		final View timepickerview = findViewById(R.id.timepicker);
		wheelTime = new WheelTime(timepickerview, type);

		// 默认选中当前时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);
		tvTitle.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});

		timepicker.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
	}

	/**
	 * 设置可以选择的时间范围
	 * 
	 * @param startYear
	 * @param endYear
	 */
	public void setRange(int startYear, int endYear)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		wheelTime.setStartYear(startYear);
		wheelTime.setEndYear(endYear);
	}

	/**
	 * 设置选中时间
	 * 
	 * @param date
	 */
	public void setTime(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		if (date == null)
			calendar.setTimeInMillis(System.currentTimeMillis());
		else
			calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		wheelTime.setPicker(year, month, day, hours, minute);
	}

	// /**
	// * 指定选中的时间，显示选择器
	// *
	// * @param date
	// */
	// public void show(Date date) {
	// Calendar calendar = Calendar.getInstance();
	// if (date == null)
	// calendar.setTimeInMillis(System.currentTimeMillis());
	// else
	// calendar.setTime(date);
	// int year = calendar.get(Calendar.YEAR);
	// int month = calendar.get(Calendar.MONTH);
	// int day = calendar.get(Calendar.DAY_OF_MONTH);
	// int hours = calendar.get(Calendar.HOUR_OF_DAY);
	// int minute = calendar.get(Calendar.MINUTE);
	// wheelTime.setPicker(year, month, day, hours, minute);
	// show();
	// }

	/**
	 * 设置是否循环滚动
	 * 
	 * @param cyclic
	 */
	public void setCyclic(boolean cyclic)
	{
		wheelTime.setCyclic(cyclic);
	}
	
	public String getText(){
		return wheelTime.getTime();
	}

	@Override
	public void onClick(View v)
	{
		String tag = (String) v.getTag();
		if (tag.equals(TAG_CANCEL))
		{
			dismiss();
			return;
		} else
		{
			if (timeSelectListener != null)
			{
				try
				{
					timeSelectListener.onTimeSelect(wheelTime.getTime());
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			dismiss();
			return;
		}
	}

	public interface OnTimeSelectListener
	{
		public void onTimeSelect(String date);
	}
	

	public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener)
	{
		this.timeSelectListener = timeSelectListener;
	}

	public void setTitle(String title)
	{
		tvTitle.setText(title);
	}
}
